package com.menghai;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageScale {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ImageScale it = new ImageScale();
		it.scaleImageFromUrl("F:/old/Penguins.jpg", "F:/new/Penguins.jpg", 55,55);
	}

		public boolean scaleImageFromUrl(String oldpath, String newpath, int wdith,int height) {  
	        try {
	            InputStream is = new FileInputStream(oldpath);
	            BufferedImage bi = ImageIO.read(is);
	            Image Itemp = bi.getScaledInstance(wdith, height,  
	                    BufferedImage.SCALE_SMOOTH);  
	            BufferedImage thumbnail = new BufferedImage(wdith, height,  
	                    BufferedImage.TYPE_INT_RGB);  
	            thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);  
	            File newimg = new File(newpath);  
	            FileOutputStream out = new FileOutputStream(newimg);  
	            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
	            JPEGEncodeParam param = encoder  
	            .getDefaultJPEGEncodeParam(thumbnail);  
	            param.setQuality(0.8f, false);
	            encoder.encode(thumbnail);
	            out.close();  
	            bi.flush();  
	            bi = null;  
	            return true;  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	            return false;  
	        }  
	    } 
 
}
